package com.zmj.sy.mom.srv.aps.shcedule;

import com.zmj.sy.mom.srv.aps.service.DxfService;
import com.zmj.sy.mom.srv.aps.service.NestService;
import com.zmj.sy.mom.srv.aps.service.PartService;
import com.zmj.sy.mom.srv.aps.service.WorkshopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
@RequiredArgsConstructor
@ConditionalOnProperty(name = "zmj.sy.serNumber", havingValue = "1")
public class SyncDxfPxdSchedule {

    private final DxfService dxfService;

    private final WorkshopService workshopService;

    private final StringRedisTemplate stringRedisTemplate;
    private final NestService nestService;
    private final PartService partService;

    @Scheduled(cron = "${scheduler.sync-dxf.ppTask}")
    public void mbdPpJob() throws IOException {
        //生成配盘任务
        LocalDate now = LocalDate.now();
        workshopService.mbdPpJob(now,null, null);
    }

    @Scheduled(cron = "${scheduler.sync-dxf.write}")
    public void getPngByPlmId() throws IOException {
        // 定时拿PlmId获取MES零件图
        UUID uuid = UUID.randomUUID();
        log.info("定时-开始-{}-定时拿PlmId获取MES零件图-getPngByPlmId-{}", uuid, LocalDateTime.now());
        try {
            dxfService.getPngByPlmId();
            log.info("定时-结束-{}-定时拿PlmId获取MES零件图-getPngByPlmId-成功", uuid);
        } catch (Exception e) {
            log.error("定时-结束-{}-定时拿PlmId获取MES零件图-getPngByPlmId-失败", uuid);
        }
    }

    @Scheduled(cron = "${scheduler.sync-dxf.write}")
    public void fillOrderPart() {
        dxfService.fillOrderPart();
    }

    @Scheduled(cron = "${scheduler.sync-dxf.write}")
    public void checkNestZeroInfo() {
        ValueOperations<String, String> valOperation = stringRedisTemplate.opsForValue();
        Boolean nest = valOperation.setIfAbsent("nest", "1", 15, TimeUnit.MINUTES);
        if(Boolean.FALSE.equals(nest)){
            return ;
        }

        // 定时获取套料检查表的可修改数据
        UUID uuid = UUID.randomUUID();
        log.info("定时-开始-{}-定时获取套料检查表的可修改数据-checkNestZeroInfo-{}", uuid, LocalDateTime.now());
        try {
            dxfService.checkNestZeroInfo();
            log.info("定时-结束-{}-定时获取套料检查表的可修改数据-checkNestZeroInfo-成功", uuid);
        } catch (Exception e) {
            log.error("定时-结束-{}-定时获取套料检查表的可修改数据-checkNestZeroInfo-失败", uuid);
        } finally {
            stringRedisTemplate.delete("nest");
        }
    }

    @Scheduled(cron = "${scheduler.sync-dxf.write}")
    public void pushPartLantek() {
        // 定时获取可推送图纸到兰特克套料软件
        UUID uuid = UUID.randomUUID();
        log.info("定时-开始-{}-定时获取可推送图纸到兰特克套料软件-pushPartLantek-{}", uuid, LocalDateTime.now());
        try {
            nestService.pushPrePartPush();
            log.info("定时-结束-{}-定时获取可推送图纸到兰特克套料软件-pushPartLantek-成功", uuid);
        } catch (Exception e) {
            log.error("定时-结束-{}-定时获取可推送图纸到兰特克套料软件-pushPartLantek-失败", uuid);
        }
    }

    @Scheduled(cron = "${scheduler.sync-dxf.check}")
    public void checkPlateNestInfo() {
        // 定时检查套料钢板套料结果存在情况
        UUID uuid = UUID.randomUUID();
        log.info("定时-开始-{}-定时检查套料钢板套料结果存在情况-checkPlateNestInfo-{}", uuid, LocalDateTime.now());
        try {
            dxfService.checkPlateNestInfo();
            log.info("定时-结束-{}-定时检查套料钢板套料结果存在情况-checkPlateNestInfo-成功", uuid);
        } catch (Exception e) {
            log.error("定时-结束-{}-定时检查套料钢板套料结果存在情况-checkPlateNestInfo-失败", uuid);
        }
    }

    @Scheduled(cron = "${scheduler.sync-dxf.deletePart}")
    public void deletePart() {
        // 定时删除无用在制品信息
        UUID uuid = UUID.randomUUID();
        log.info("定时-开始-{}-定时删除无用在制品信息-deletePart-{}", uuid, LocalDateTime.now());
        try {
            partService.deletePart();
            log.info("定时-结束-{}-定时删除无用在制品信息-deletePart-成功", uuid);
        } catch (Exception e) {
            log.error("定时-结束-{}-定时删除无用在制品信息-deletePart-失败", uuid);
        }
    }

}
